package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.file.service.FileStorageService;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.WmMaterialDto;
import com.heima.model.wemedia.pojos.WmMaterial;
import com.heima.model.wemedia.pojos.WmUser;
import com.heima.utils.thread.WmThreadLocalUtil;
import com.heima.wemedia.mapper.WmMaterialMapper;
import com.heima.wemedia.service.WmMaterialService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.UUID;


@Slf4j
@Service
@Transactional
public class WmMaterialServiceImpl extends ServiceImpl<WmMaterialMapper, WmMaterial> implements WmMaterialService {

    @Autowired
    private FileStorageService fileStorageService;

    @Override
    public ResponseResult uploadPicture(MultipartFile multipartFile) {
        // 1. 检查参数
        if (multipartFile == null || multipartFile.getSize() == 0){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_IMAGE_FORMAT_ERROR);
        }
        String fileId = null;
        try {
            // 2. 上传图片到minio中
            String fileName = UUID.randomUUID().toString();
            String originalFileName = multipartFile.getOriginalFilename();
            String postfix = originalFileName.substring(originalFileName.lastIndexOf('.'));
            fileId = fileStorageService.uploadImgFile("", fileName + postfix, multipartFile.getInputStream());
            log.info("上传图片到minio中");
        } catch (IOException e) {
            log.error("WmMaterialServiceImpl上传文件失败");
            throw new RuntimeException(e);
        }
        // 3. 将图片地址保存到数据库
        WmMaterial wmMaterial = new WmMaterial();
        wmMaterial.setUserId(WmThreadLocalUtil.getUser().getId());
        wmMaterial.setUrl(fileId);
        wmMaterial.setIsCollection((short)0);
        wmMaterial.setType((short)0);
        wmMaterial.setCreatedTime(new Date());
        save(wmMaterial);

        return ResponseResult.okResult(wmMaterial);
    }

    @Override
    public ResponseResult findList(WmMaterialDto dto) {
        if (dto.getPage() == null){
            dto.setPage(1);
        }
        if (dto.getSize() == null){
            dto.setSize(10);
        }
        Page<WmMaterial> page = new Page<>(dto.getPage(),dto.getSize());
        page.addOrder(new OrderItem("created_time",false));

        WmUser user = WmThreadLocalUtil.getUser();

        Page<WmMaterial> p = lambdaQuery()
                .eq(dto.getIsCollection() == 1 ,WmMaterial::getIsCollection, dto.getIsCollection())
                .eq(WmMaterial::getUserId, user.getId())
                .page(page);

        ResponseResult result = new PageResponseResult(dto.getPage(),dto.getSize(), (int) p.getTotal());
        result.setData(p.getRecords());
        return result;
    }
}
